package com.hzw.video.opengl

import android.graphics.SurfaceTexture
import android.os.Bundle
import android.view.Surface
import androidx.appcompat.app.AppCompatActivity
import com.hzw.video.R
import com.hzw.video.decoder.AudioDecoder
import com.hzw.video.decoder.VideoDecoder
import com.hzw.video.opengl.drawer.VideoDrawer
import kotlinx.android.synthetic.main.activity_open_gl.*
import java.util.concurrent.Executors

class OpenGlActivity : AppCompatActivity() {


    lateinit var drawer: IDrawer

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_open_gl)
//        drawer = TriangleDrawer()
//        drawer = BitmapDrawer(BitmapFactory.decodeResource(resources, R.drawable.timg))

        drawer = VideoDrawer()
        drawer.getSurfaceTexture {
            initPlayer(it)
        }
        initRender()
    }

    private fun initPlayer(surfaceTexture: SurfaceTexture) {
        val videoPath = getExternalFilesDir("video")?.absolutePath + "/jl.mp4"
        val threadPool = Executors.newFixedThreadPool(2)
        threadPool.execute(VideoDecoder(videoPath, null, Surface(surfaceTexture)))
        threadPool.execute(AudioDecoder(videoPath))
    }

    private fun initRender() {
        glSurfaceView.setEGLContextClientVersion(2)
        glSurfaceView.setRenderer(SimpleRender(drawer))
    }

    override fun onDestroy() {
        drawer.release()
        super.onDestroy()
    }
}